package com.example.im.ui.fragment

import android.content.Context
import android.content.Intent
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.im.common.Enumerate
import com.example.im.R
import com.example.im.bean.ChatEntry
import com.example.im.bean.EventBusData
import com.example.im.bean.UserRoom
import com.example.im.common.Constants
import com.example.im.db.DatabaseStorage
import com.example.im.mvp.base.BasePresenter
import com.example.im.ui.abstracts.AbstractBaseFragment
import com.example.im.ui.activity.ChatActivity2
import com.example.im.ui.activity.SearchActivity
import com.example.im.ui.adapter.ChatUserAdapter2
import kotlinx.android.synthetic.main.fragment_main_chat.*
import kotlinx.android.synthetic.main.fragment_main_chat.linear_search
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class MainChatFragment : AbstractBaseFragment<BasePresenter<*>>(),
    ChatUserAdapter2.OnClickItemListener {

    private var entrys = mutableListOf<ChatEntry>()
    private var adapter: ChatUserAdapter2? = null

    init {
        val chatEntry = DatabaseStorage.getChatEntryStorage()
        entrys.addAll(chatEntry)
    }

    override val layoutId: Int
        get() = R.layout.fragment_main_chat

    override fun initView() {

        EventBus.getDefault().register(this)

        adapter = ChatUserAdapter2((activity as Context?)!!, entrys)
        adapter!!.setOnItemClickListener(this)
        recycler_view_chat.layoutManager = LinearLayoutManager(activity)
        recycler_view_chat.itemAnimator = null
        recycler_view_chat.adapter = adapter

        linear_search.setOnClickListener {
            activity!!.startActivity(Intent(activity, SearchActivity::class.java))
        }
    }

    override fun initPresenter() {

    }

    override fun initLazyLoad() {

    }

    override fun onDestroyView() {
        super.onDestroyView()
        EventBus.getDefault().unregister(this)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEventMainThread(eventBus: EventBusData<*>) {
        if (eventBus.type ?: return != Enumerate.Handle.list_chat)
            return
        val data = eventBus.data ?: return
        when (eventBus.action ?: return) {
            Enumerate.Action.list_add -> {
                if (data is ChatEntry) adapter!!.addDataItem(data)
            }
            Enumerate.Action.list_updata -> {
                if (data is ChatEntry) adapter!!.upDataItem(data)
            }
            Enumerate.Action.list_delete -> {
                if (data is UserRoom) adapter!!.removeDataItem(data)
            }
            Enumerate.Action.list_clear -> {
                adapter!!.clearData()
            }
            else -> return
        }
//        when (eventBus.action ?: return) {
//            Enumerate.Action.list_add -> {
//                val map = (eventBus.data ?: return) as MutableMap.MutableEntry<*, *>
//                adapter!!.addDataItem(map.key as ChatRecordLast)
//            }
//            Enumerate.Action.list_updata -> {
//                val last = (eventBus.data ?: return) as ChatRecordLast
//                adapter!!.upDataItem(last)
//            }
//            Enumerate.Action.list_delete -> {
//                val friend = (eventBus.data ?: return) as FriendOrRoom
//                adapter!!.removeDataItem(friend.id)
//                onHint("已删除聊天记录")
//            }
//            Enumerate.Action.list_clear -> {
//                adapter!!.clearData()
//            }
//            else -> {
//            }
//        }
    }

    override fun onClickItem(view: View, position: Int) {
        val intent = Intent(activity, ChatActivity2::class.java)
        intent.putExtra(Constants.BUNDLE_T0_CHAT_DATA, entrys[position].userRoom)
        startActivity(intent)
    }
}